
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const alerts = n =>{
  console.log(n);
}

String.prototype.RHtml = function () {
  return this.replace(/<[^>]+>/g, "").replace(/&nbsp;/g, "");
};

String.prototype.ReplaceAll = function (s1, s2) {
  return this.replace(new RegExp(s1, "gm"), s2);
};

Array.prototype.Remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

Array.prototype.RemoveAt = function (n) {
  if (n < 0)
  { return this; }
  else
  { return this.slice(0, n).concat(this.slice(n + 1, this.length)); }
};

Array.prototype.indexOf = function (val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
  }
  return -1;
};

Array.prototype.Max = function () {
  return Math.max.apply(Math, this);
};

Array.prototype.Min = function () {
  return Math.min.apply(Math, this);
};

String.prototype.ToDate = function () {
  var date = new Date(this.replace(/-/g, "/"));
  return date.Format("MM.dd");
};

Date.prototype.Format = function (format) {
  var o = {
    "M+": this.getMonth() + 1, //month 
    "d+": this.getDate(), //day 
    "h+": this.getHours(), //hour 
    "m+": this.getMinutes(), //minute 
    "s+": this.getSeconds(), //second 
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
    "S": this.getMilliseconds() //millisecond 
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
};
function Upload(path, data, callback) {
  wx.uploadFile({
    url: domain + '/tools/Upload.ashx',
    filePath: path,
    name: 'img',
    formData: data,
    success: function (res) {
      if (callback) {
        callback(res.data)
      }
    }
  });
}
function Error(title) {
  wx.showToast({
    title: title,
    image: '/static/img/error.png'
  });
};
function Success(title, callback) {
  wx.showToast({
    title: title,
    success: function () {
      if (callback) {
        setTimeout(function () {
          callback();
        }, 1000);
      }
    }
  });
};
function Loading(title) {
  wx.showLoading({
    title: title
  });
};
function Confirm(title, callback) {
  wx.showModal({
    title: '提示',
    content: title,
    success: function (res) {
      if (res.confirm && callback) {
        callback();
      }
    }
  });
};
function Alert(title, callback) {
  wx.showModal({
    title: '提示',
    content: title,
    showCancel: false,
    success(res) {
      if (res.confirm && callback) {
        callback();
      }
    }
  })
};
function GoLogin() {
  var userid = wx.getStorageSync('userid') || '';
  if (userid == '') {
    wx.hideLoading();
    wx.navigateTo({
      url: '/pages/login/login',
    })
    return true;
  } else {
    return false;
  }

};

module.exports = {
  formatTime: formatTime,
  alerts:alerts,
  Upload: Upload,
  Success: Success,
  Error: Error,
  Loading: Loading,
  Confirm: Confirm,
  Alert: Alert,
  GoLogin: GoLogin
}
